import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import DocumentManager from '@/views/DocumentManager.vue'
import KnowledgeExplorer from '@/views/KnowledgeExplorer.vue'
import GraphVisualization from '@/views/GraphVisualization.vue'
import SearchChart from "@/views/SearchChart.vue";
import UserManage from "@/views/manager/user/UserManage.vue";
import ManageMain from "@/views/manager/ManageMain.vue";
import SearchHistoryManage from "@/views/manager/search-history/SearchHistoryManage.vue";
import MyPage from "@/views/MyPage.vue";
import Abstract from "@/views/Abstract.vue";
import GraphManage from "@/views/manager/graph/GraphManage.vue";
import DocumentManage from "@/views/manager/document/DocumentManage.vue";
import FeedbackManage from "@/views/manager/feedback/FeedbackManage.vue";
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true }
  },
  {
    path: '/document',
    name: 'DocumentManager',
    component: DocumentManager,
    meta: { 
      requiresAuth: true,
      requiresAdmin: true
    }
  },
  {
    path: '/knowledge',
    name: 'KnowledgeExplorer',
    component: KnowledgeExplorer,
    meta: { requiresAuth: true }
  },
  {
    path: '/graph',
    name: 'GraphVisualization',
    component: GraphVisualization,
    meta: { requiresAuth: true }
  },
  {
    path: '/searchChart',
    name: 'SearchChart',
    component: SearchChart,
    meta: { requiresAuth: true }
  },
  {
    path: '/Abstract',
    name: 'Abstract',
    component: Abstract,
    meta: { requiresAuth: true }
  },
  {
    path: '/manageMain',
    name: 'ManageMain',
    component: ManageMain,
    redirect: '/manageMain/user', // 添加这行
    meta: { requiresAuth: true },
    children: [
      {
        path: 'user', // 子路由路径相对于父路由（不需要加 /）
        name: 'UserManage',
        component: UserManage,
        meta: { requiresAdmin: true }
      },
      {
        path: 'search-history', // 子路由路径相对于父路由（不需要加 /）
        name: 'SearchHistoryManage',
        component: SearchHistoryManage,
        meta: { requiresAdmin: true }
      },
      {
        path: 'graph', // 子路由路径相对于父路由（不需要加 /）
        name: 'GraphManage',
        component: GraphManage,
        meta: { requiresAdmin: true }
      },
      {
        path: 'document', // 子路由路径相对于父路由（不需要加 /）
        name: 'DocumentManage',
        component: DocumentManage,
        meta: { requiresAdmin: true }
      },
      {
        path: 'feedback', // 子路由路径相对于父路由（不需要加 /）
        name: 'FeedbackManage',
        component: FeedbackManage,
        meta: { requiresAdmin: true }
      },
    ]
  },
  {
    path: '/my-page',
    name: 'MyPage',
    component: MyPage,
    meta: { requiresAuth: true }
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const isAdmin = localStorage.getItem('isAdmin') === 'true'
  
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else if (!to.meta.requiresAuth && token) {
    next('/')
  } else if (to.meta.requiresAdmin && !isAdmin) {
    next('/')
    ElMessage.error('需要管理员权限访问此页面')
  } else {
    next()
  }
})

export default router 